//
//  EulerRotation.m
//  NinevehglTest
//
//  Created by 郝建林 on 2016/10/18.
//  Copyright © 2016年 CoolTools. All rights reserved.
//

#import "EulerRotation.h"

@interface EulerRotation()
{
    float _rx, _ry, _rz;
}
@end

@implementation EulerRotation

@synthesize view;

- (id)init {
    if(self = [super init]) {
        _rx = 0;
        _ry = 0;
        _rz = 0;
    }
    return self;
}

- (float)rx {
    return _rx;
}

- (float)ry {
    return _ry;
}

- (float)rz {
    return _rz;
}

- (void)setRx:(float)rx {
    _rx = rx;
    [self updateSlider:SliderX ofValue:rx];
    [self updateLabel:SliderX ofValue:rx];
}

- (void)setRy:(float)ry {
    _ry = ry;
    [self updateSlider:SliderY ofValue:ry];
    [self updateLabel:SliderY ofValue:ry];
}

- (void)setRz:(float)rz {
    _rz = rz;
    [self updateSlider:SliderZ ofValue:rz];
    [self updateLabel:SliderZ ofValue:rz];
}

- (void)reset {
    self.rx = 0;
    self.ry = 0;
    self.rz = 0;
}

- (void)onSliderChanged: (NSInteger)sliderIndex value: (float)sliderValue {
    float angle = [self angleOfSliderValue:sliderValue];
    //update value
    switch(sliderIndex) {
        case SliderX:
            _rx = angle;
            break;
        case SliderY:
            _ry = angle;
            break;
        case SliderZ:
            _rz = angle;
            break;
    }
    //update label
    [self updateLabel:sliderIndex ofValue:angle];
}

- (float)angleOfSliderValue: (float)sliderValue {
    return (sliderValue * 2 - 1) * 180;
}

- (void)updateSlider: (NSInteger)index ofValue: (float)value
{
    UISlider *slider = [self.view viewWithTag:SliderTagBase+index];
    slider.value = (value / 180 + 1) / 2;
}

- (void)updateLabel: (NSInteger)index ofValue: (float)value
{
    UILabel *label = (UILabel*)[self.view viewWithTag:index+LabelTagBase];
    label.text = [NSString stringWithFormat:@"%.0f", value];
}

@end
